from typing import List

from fastapi import FastAPI, Request
from pydantic import BaseModel
from fastapi.responses import JSONResponse

app = FastAPI()

class UnexpectException(Exception):
    def __init__(self, name: str):
        self.name = name

class Item(BaseModel):
    name: str
    description: str

@app.exception_handler(UnexpectException)
async def unexpect_exception_handler(request: Request,exc: UnexpectException):
    return JSONResponse(status_code=400, content="发生错误！")

items = [
    {"name": "Foo", "description": "There comes my hero"},
    {"name": "Red", "description": "It's my aeroplane"},
]


@app.get("/items/", response_model=List[Item])
async def read_items():
    return items


@app.get("/exception")
async def excetion_demo(name: str):
    if name=='wa':
        raise UnexpectException("wahaha")
    return {"data": "success"}